//
//  NewTopic.m
//  LilyBBS
//
//  Created by 李行 on 11-12-5.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "NewTopic.h"
#import "ASIFormDataRequest.h"
#import "SingleCookie.h"
#import "RegexKitLite.h"
#import "Reachability.h"

@implementation NewTopic

@synthesize title,content,boardName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)done{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.nju.edu.cn"];
    switch ([r currentReachabilityStatus]) {
        case NotReachable:
            //没有网络连接
            NSLog(@" ");
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络好像连接不上。。。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [alert show];
            [alert release];
            return;
        case ReachableViaWWAN:
            //使用3G网络            
            break;
        case ReachableViaWiFi:
            // 使用WiFi网络            
            break;
    }
    
    //标题不能为空
    if([title.text isEqualToString:@""]){
		UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"发表主题提示" message:@"请输入正确的标题！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        [self.title becomeFirstResponder];
		return;
	}
    
    NSString* requestURLString = [NSString stringWithFormat:@"http://bbs.nju.edu.cn/vd%@/bbssnd?board=%@",[SingleCookie sharedSingleCookie].randomNumber,self.boardName];
    NSString* topicTitle = title.text;
    NSString* topicContent = content.text;
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); 
    NSURL* requestURL = [NSURL URLWithString:requestURLString];
    
    ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:requestURL];
    [request setStringEncoding:enc];
    [request setUseCookiePersistence:NO];
    [request setRequestCookies:[SingleCookie sharedSingleCookie].savedCookies];
    [request setPostValue:topicTitle forKey:@"title"];
    [request setPostValue:@"0" forKey:@"pid"];
    [request setPostValue:@"0" forKey:@"reid"];
    [request setPostValue:@"1" forKey:@"signature"];
    [request setPostValue:@"on" forKey:@"autocr"];
    [request setPostValue:topicContent forKey:@"text"];
    [request startSynchronous];
    NSError *error = [request error];
    NSString *response;
    if (!error) {
        response = [request responseString];
        if ([response isMatchedByRegex:@"url"]) {
            NSLog(@"发送成功!");
        }else{
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"发表主题提示" message:@"Sorry，没能发表成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
            return;
        }
    }else{
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"发表主题提示" message:@"Sorry，没能发表成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return;
    }
    [self dismissModalViewControllerAnimated:YES];
}

-(void)cancel{
    [self dismissModalViewControllerAnimated:YES];
}

@end
